package hongcaosp.app.android.modle;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;

public abstract class DataCallBack<T> extends AbsCallback<BaseResponse> {
    private static Map<Integer,ErrorHandler> handlerCollection = new HashMap<>();

    @Override
    public final void onSuccess(final Response<BaseResponse> response) {
        BaseResponse dataResponse = response.body();
        if (dataResponse != null) {
            if (dataResponse.getCode() == 0) {
                if (!TextUtils.isEmpty(dataResponse.getData())) {
                    T data;
                    try {
                        data = JSON.parseObject(dataResponse.getData(), ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
                    } catch (Exception e) {
                        data = (T) dataResponse.getData();
                    }

                    onGetData(data);
                } else {
                    onGetData(null);
                }
            } else {
                if (handlerCollection.containsKey(dataResponse.getCode())) {
                    ErrorHandler errorHandler = handlerCollection.get(dataResponse.getCode());
                    errorHandler.onHandle();
                } else {
                    onFail(dataResponse.getCode(), dataResponse.getMessage());
                }
            }
        } else {
            if (handlerCollection.containsKey(response.code())) {
                ErrorHandler errorHandler = handlerCollection.get(response.code());
                errorHandler.onHandle();
            } else {
                onFail(response.code(), response.message());
            }
        }

    }

    @Override
    public BaseResponse convertResponse(okhttp3.Response response) throws Throwable {
        try {
            BaseResponse baseResponse = JSON.parseObject(response.body().string(), BaseResponse.class);
            return baseResponse;
        } catch (Throwable e) {
            throw e;
        }
    }

    public void onGetData(T data) {

    }

    public void onFail(int code, String errorMessage) {

    }

    @Override
    public final void onCacheSuccess(final Response<BaseResponse> response) {
        super.onCacheSuccess(response);
        BaseResponse dataResponse = response.body();
        if (dataResponse != null) {
            if (dataResponse.getCode() == 0) {
                if (!TextUtils.isEmpty(dataResponse.getData())) {
                    T data = JSON.parseObject(dataResponse.getData(), ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
                    onGetData(data);
                } else {
                    onGetData(null);
                }
            } else {
                onFail(dataResponse.getCode(), dataResponse.getMessage());
            }
        } else {
            onFail(response.code(), response.message());
        }
    }

    @Override
    public final void onError(final Response<BaseResponse> response) {
        super.onError(response);
        if (handlerCollection.containsKey(response.code())) {
            ErrorHandler errorHandler = handlerCollection.get(response.code());
            errorHandler.onHandle();
        } else {
            onFail(response.code(), response.message());
        }

    }

    public static void addErorrHandler(int code,ErrorHandler errorHandler) {
        handlerCollection.put(code, errorHandler);
    }
}
